(async () => {
    const Koa = require('koa');
    const KoaStaticCache = require('koa-static-cache');
    const KoaBodyParser = require('koa-bodyparser');
    const router = require('./routers/main');
    const Session = require('koa-session');

    const app = new Koa();

    // 密钥
    app.keys = ['sw_lance'];
    const CONFIG  = {
        key: 'koa:sess',
        maxAge: 86400000,
        autoCommit: true,
        overwrite: true,
        httpOnly: true,
        signed: true,
        rolling: false,
        renew: false,
    };
    app.use(Session(CONFIG, app));

    // 第一种解决跨域问题方案
    app.use(async (ctx, next) => {
        ctx.set("Access-Control-Allow-Origin", "http://127.0.0.1:8081");
        ctx.set('Access-Control-Allow-Credentials', true);
        await next();
    });

    app.use(KoaStaticCache('./public', {
        prefix: 'public',
        gzip: true
    }));

    app.use(KoaBodyParser());
    app.use(router.routes());

    app.listen(80);
})();